<%@page import="java.text.SimpleDateFormat"%>
<%@page import="markan.fb.TCFacebookHelper"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*, java.util.*" %>    
<%@page import="markan.*"%>
<%
	int currentPage = TCStringUtil.string2Integer(request.getParameter("pageIndex"), 1); // 현재페이지
	int facebookId = TCStringUtil.string2Integer(request.getParameter("id")); 

	Map result = new HashMap();
	
	List resultList = new ArrayList();
	Connection conn = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	try {
		conn = new markan.db.TCConnection().getConnection();
		
		String sql = "";
		sql =	"SELECT FACEBOOK_ID, FACEBOOK_EMAIL, date_format(FRST_REGIST_PNTTM,'%Y-%m-%d') as FRST_REGIST_PNTTM, "
				+ "FACEBOOK_PASSWORD, FACEBOOK_ACCESS_TOKEN, FACEBOOK_ACCESS_EXPIRES "
				//+ "FROM FACEBOOK_USER WHERE USE_AT = 'Y' AND FACEBOOK_ID = " + facebookId;
				+ "FROM FACEBOOK_USER WHERE USE_AT = 'Y' AND FACEBOOK_ID = ?";
		//out.println(sql);
		pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, facebookId);
		rs = pstmt.executeQuery();
		if(rs.next()) {
			String FACEBOOK_ID = rs.getString("FACEBOOK_ID");
			String FACEBOOK_EMAIL = rs.getString("FACEBOOK_EMAIL");
			String FRST_REGIST_PNTTM = rs.getString("FRST_REGIST_PNTTM"); 
			String FACEBOOK_PASSWORD = rs.getString("FACEBOOK_PASSWORD"); 
			String FACEBOOK_ACCESS_TOKEN = rs.getString("FACEBOOK_ACCESS_TOKEN"); 
			String FACEBOOK_ACCESS_EXPIRES = rs.getString("FACEBOOK_ACCESS_EXPIRES"); 
			
			result.put("FACEBOOK_ID", FACEBOOK_ID);
			result.put("FACEBOOK_EMAIL", FACEBOOK_EMAIL);
			result.put("FRST_REGIST_PNTTM", FRST_REGIST_PNTTM);
			result.put("FACEBOOK_PASSWORD", FACEBOOK_PASSWORD);
			result.put("FACEBOOK_ACCESS_TOKEN", FACEBOOK_ACCESS_TOKEN);
			result.put("FACEBOOK_ACCESS_EXPIRES", FACEBOOK_ACCESS_EXPIRES);
		}
	} catch(SQLException e) {
		e.printStackTrace();
	} catch(Exception e) {
		e.printStackTrace();
	} finally {
		if(rs!=null) rs.close();
		if(pstmt!=null) pstmt.close();
		if(conn!=null) conn.close();
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>페이스북 사용자</title>
</head>
<body>
	<table border="1">
		<tr>
			<th>페이스북 이메일</th>
			<td><%=result.get("FACEBOOK_EMAIL")%></td>
		</tr>
		<tr>
			<th>페이스북 암호</th>
			<td><%=result.get("FACEBOOK_PASSWORD")%></td>
		</tr>
		<tr>
			<th>등록일</th>
			<td><%=result.get("FRST_REGIST_PNTTM")%></td>
		</tr>
		<tr>
			<th>페이스북 토큰</th>
			<td>
				<%if(result.get("FACEBOOK_ACCESS_TOKEN")!=null){ %>
					<%=result.get("FACEBOOK_ACCESS_TOKEN")%>
				<%}else{ %>
					<a href="<%=TCFacebookHelper.getOuathUrl(facebookId)%>">페이스북 인증</a>
				<%} %>
			</td>
		</tr>
		<tr>
			<th>페이스북 토큰 유효기간</th>
			<td>
				<%if(result.get("FACEBOOK_ACCESS_TOKEN")!=null){ %>
					<%=result.get("FACEBOOK_ACCESS_EXPIRES")%>
					<%-- <%
						long accessExpires = Long.parseLong((String)result.get("FACEBOOK_ACCESS_EXPIRES"));
						accessExpires = System.currentTimeMillis() + accessExpires;
						SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						out.print(format.format(new java.util.Date(accessExpires)));
					%> --%>
					<a href="<%=TCFacebookHelper.getOuathUrl(facebookId)%>">페이스북 토큰 갱신</a>
				<%} %>
			</td>
		</tr>
		<tr>
			<td></td>
			<td><a href="javascript:history.back();">목록</a></td>
		</tr>
	</table>
</body>
</html>